﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LS.Common;
using LS.Entitys;
namespace LS.Cores
{
    /// <summary>
    /// 提供角色管理服务
    /// </summary>

    public class SysRoleService : BaseService<SysRoleEntity>, ISysRole
    {

        public IList<SysRoleMenuEntity> QueryRoleMenu(string strwhere)
        {
            string sql = "select * From  sys_rolemenu where 1=1 ";
            if (!string.IsNullOrEmpty(strwhere))
            {
                sql += strwhere;
            }
            return SqlHelper.Query<SysRoleMenuEntity>(sql).ToList();
        }

        public int InsertRoleMenu(SysRoleEntity model, List<SysRoleMenuEntity> listModel)
        {
            bool resulte = SqlHelper.ExecuteTranSql((con, tran) =>
            {
                int r = 0;
                try
                {
                    StringBuilder strsql = new StringBuilder();
                    strsql.Append("insert into sys_role(rolecode,rolename,roleseq,remark,createby,createdate,Isdel,isenable) ");
                    strsql.Append(" values(@rolecode,@rolename,@roleseq,@remark,@createby,@createdate,@isdel,@isenable)");

                    r = SqlHelper.ExecuteCon(strsql.ToString(), model, tran, null, CommandType.Text, con);
                    

                    if (listModel != null && listModel.Count > 0)
                    {
                        StringBuilder strsqlfun = new StringBuilder();
                        strsqlfun.Append("insert into sys_rolemenu(menucode,rolecode,funcode) ");
                        strsqlfun.Append(" values(@menucode,@rolecode,@funcode)");
                        r = SqlHelper.ExecuteCon(strsqlfun.ToString(), listModel, tran, null, CommandType.Text, con);
                    }
                }
                catch (Exception ex)
                {
                    Log4NetHelper.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType).Debug(ex.Message);
                    r = 0;
                }

                return r;
            });
            return resulte ? 1 : 0;
        }

        public int UpdateRoleMenu(SysRoleEntity model, List<SysRoleMenuEntity> listModel)
        {
            bool resulte = SqlHelper.ExecuteTranSql((con, tran) =>
            {
                int r = 0;
                try
                {
                    StringBuilder strsql = new StringBuilder();
                    strsql.Append("update sys_role set  ");
                    strsql.Append("rolecode=@rolecode,rolename=@rolename,roleseq=@roleseq,remark=@remark,createby=@createby,createdate=@createdate,Isdel=@Isdel,isenable=@isenable ");
                    strsql.Append(" where id=@id ");
                    r = SqlHelper.ExecuteCon(strsql.ToString(), model, tran, null, CommandType.Text, con);

                    if (listModel != null && listModel.Count > 0)
                    {
                        string sqlrolemenu = "delete from  sys_rolemenu where rolecode=@Rolecode";
                        r = SqlHelper.ExecuteCon(sqlrolemenu.ToString(), new { Rolecode = model.RoleCode }, tran, null, CommandType.Text, con);
                        StringBuilder strsqlfun = new StringBuilder();
                        strsqlfun.Append("insert into sys_rolemenu(menucode,rolecode,funcode) ");
                        strsqlfun.Append(" values(@menucode,@rolecode,@funcode)");
                        r = SqlHelper.ExecuteCon(strsqlfun.ToString(), listModel, tran, null, CommandType.Text, con);
                    }
                }
                catch (Exception ex)
                {
                    Log4NetHelper.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType).Debug(ex.Message);
                    r = 0;
                }

                return r;
            });
            return resulte ? 1 : 0;
        }

        public bool ExistsRoleCode(string id, string roleCode)
        {
            StringBuilder strsql = new StringBuilder();
            strsql.Append("select count(1)  From  sys_role where Isdel=0 ");
            if (!string.IsNullOrEmpty(id))
            {
                strsql.Append(" and  id!='" + id + "'");
            }
            if (!string.IsNullOrEmpty(roleCode))
            {
                strsql.Append(" and  rolecode='" + roleCode + "'");
            }
            return SqlHelper.ExecuteScalar<int>(strsql.ToString()) > 0 ? true : false;
        }
    }
}
